package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController {

    @DubboReference
    MenuService menuService;

    /**
     * @Author: 涵哥
     * @Date: 19:33 2021/1/19
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 菜单-分页展示
    */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("[菜单-分页查询]data:{}",queryPageBean);
        PageResult pageResult = menuService.findPage(queryPageBean);
        return new Result(true,MessageConst.ACTION_SUCCESS,pageResult);

    }

    /**
     * @Author: 涵哥
     * @Date: 20:22 2021/1/19
     * @Parms [menu]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 菜单-新增
    */
    @PostMapping("/add")
    public Result add(@RequestBody Menu menu){
        log.info("[菜单-新增]data: {}",menu);
        menuService.add(menu);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    /**
     * @Author: 涵哥
     * @Date: 20:25 2021/1/19
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 菜单-删除
    */
    @PostMapping("/delete")
    public Result delete(Integer id){
        log.info("[菜单-删除]data: {}",id);
        menuService.delete(id);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    /**
     * @Author: 涵哥
     * @Date: 20:37 2021/1/19
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 菜单-编辑
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        log.info("[菜单-编辑]data: {}",menu);
        menuService.edit(menu);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    /**
     * @Author: 涵哥
     * @Date: 21:09 2021/1/19
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 据id查询-编辑回显数据
    */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        Menu menu = menuService.findById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, menu);
    }

    @GetMapping("showMenu")
    public Result showMenu(){
        log.info("展示-菜单信息");
        List<Map<String,Object>> arrayList = menuService.showMenu();
        if (arrayList.isEmpty()){
            return new Result(false,MessageConst.GET_MENU_FAIL);
        }
        return new Result(true,MessageConst.GET_MENU_SUCCESS,arrayList);
    }

    @GetMapping("showMenuTwo")
    public Result showMenuTwo(String username){
        List<Menu> arrayList = menuService.showMenuTwo(username);
        if (arrayList.isEmpty()){
            return new Result(false,MessageConst.GET_MENU_FAIL);
        }
        return new Result(true,MessageConst.GET_MENU_SUCCESS,arrayList);
    }
}
